package com.hibox.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.hibox.common.core.domain.AjaxResult;
import com.hibox.common.exception.UtilException;
import com.hibox.common.utils.http.HttpUtils;

import java.util.Map;

/**
 * @author WangFK
 * @version 1.0
 * @since 2023/8/18 10:00
 */
public class PhoneAddress {
    public static String getPhoneAddress(String phone) {
        if (!PhoneUtil.isPhone(phone)) {
            throw new UtilException("手机号格式错误");
        }
        String baseUrl = "https://www.mxnzp.com/api/mobile_location/aim_mobile";
        String params = "mobile=" + phone + "&app_id=idgorwnnojpowkrl&app_secret=wDKi9GfjUSlvAKb5ftYmV0IoB02PU5ys";

        AjaxResult ajaxResult = JSONUtil.toBean(HttpUtils.sendGet(baseUrl, params), AjaxResult.class);

        Map<String, String> data = Convert.toMap(String.class, String.class, ajaxResult.get("data"));

        return data.get("province");
    }
}
